﻿
// Author:					Doan Quoc Tuan
// Created:					2013-5-21
// Last Modified:			2013-5-21
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;

namespace SMSPanasonic.Data
{

    public static class DBSmsMtStatistic
    {
        /// <summary>
        /// Gets the connection string for read.
        /// </summary>
        /// <returns></returns>
        private static string GetReadConnectionString()
        {
            return Properties.Settings.Default.ConnectionString;

        }

        /// <summary>
        /// Gets the connection string for write.
        /// </summary>
        /// <returns></returns>
        private static string GetWriteConnectionString()
        {

            return Properties.Settings.Default.ConnectionString;

        }


        /// <summary>
        /// Inserts a row in the SmsMtStatistic table. Returns rows affected count.
        /// </summary>
        /// <param name="mtGuid"> mtGuid </param>
        /// <param name="moGuid"> moGuid </param>
        /// <param name="shortCode"> shortCode </param>
        /// <param name="destAddr"> destAddr </param>
        /// <param name="operatorID"> operatorID </param>
        /// <param name="message"> message </param>
        /// <param name="cooperateID"> cooperateID </param>
        /// <param name="status"> status </param>
        /// <param name="sentTime"> sentTime </param>
        /// <param name="cDRIndicator"> cDRIndicator </param>
        /// <param name="mTType"> mTType </param>
        /// <param name="retryOnFailReqTimes"> retryOnFailReqTimes </param>
        /// <param name="retryOnTimeOutReqTimes"> retryOnTimeOutReqTimes </param>
        /// <param name="retryOnFailReqDuration"> retryOnFailReqDuration </param>
        /// <param name="retryOnTimeOutReqDuration"> retryOnTimeOutReqDuration </param>
        /// <returns>int</returns>
        public static int Create(
            SqlGuid mtGuid,
            SqlGuid moGuid,
            SqlString shortCode,
            SqlString destAddr,
            SqlInt32 operatorID,
            SqlString message,
            SqlInt32 cooperateID,
            SqlString status,
            SqlDateTime sentTime,
            SqlString cDRIndicator,
            SqlString mTType,
            SqlInt32 retryOnFailReqTimes,
            SqlInt32 retryOnTimeOutReqTimes,
            SqlInt64 retryOnFailReqDuration,
            SqlInt64 retryOnTimeOutReqDuration)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "SmsMtStatistic_Insert", 15);
            sph.DefineSqlParameter("@MtGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, mtGuid);
            sph.DefineSqlParameter("@MoGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, moGuid);
            sph.DefineSqlParameter("@ShortCode", SqlDbType.VarChar, 50, ParameterDirection.Input, shortCode);
            sph.DefineSqlParameter("@DestAddr", SqlDbType.VarChar, 15, ParameterDirection.Input, destAddr);
            sph.DefineSqlParameter("@OperatorID", SqlDbType.Int, ParameterDirection.Input, operatorID);
            sph.DefineSqlParameter("@Message", SqlDbType.NVarChar, 1000, ParameterDirection.Input, message);
            sph.DefineSqlParameter("@CooperateID", SqlDbType.Int, ParameterDirection.Input, cooperateID);
            sph.DefineSqlParameter("@Status", SqlDbType.VarChar, 50, ParameterDirection.Input, status);
            sph.DefineSqlParameter("@SentTime", SqlDbType.DateTime, ParameterDirection.Input, sentTime);
            sph.DefineSqlParameter("@CDRIndicator", SqlDbType.VarChar, 15, ParameterDirection.Input, cDRIndicator);
            sph.DefineSqlParameter("@MTType", SqlDbType.VarChar, 5, ParameterDirection.Input, mTType);
            sph.DefineSqlParameter("@RetryOnFailReqTimes", SqlDbType.Int, ParameterDirection.Input, retryOnFailReqTimes);
            sph.DefineSqlParameter("@RetryOnTimeOutReqTimes", SqlDbType.Int, ParameterDirection.Input, retryOnTimeOutReqTimes);
            sph.DefineSqlParameter("@RetryOnFailReqDuration", SqlDbType.BigInt, ParameterDirection.Input, retryOnFailReqDuration);
            sph.DefineSqlParameter("@RetryOnTimeOutReqDuration", SqlDbType.BigInt, ParameterDirection.Input, retryOnTimeOutReqDuration);
            int rowsAffected = sph.ExecuteNonQuery();
            return rowsAffected;

        }


        /// <summary>
        /// Updates a row in the SmsMtStatistic table. Returns true if row updated.
        /// </summary>
        /// <param name="mtGuid"> mtGuid </param>
        /// <param name="moGuid"> moGuid </param>
        /// <param name="shortCode"> shortCode </param>
        /// <param name="destAddr"> destAddr </param>
        /// <param name="operatorID"> operatorID </param>
        /// <param name="message"> message </param>
        /// <param name="cooperateID"> cooperateID </param>
        /// <param name="status"> status </param>
        /// <param name="sentTime"> sentTime </param>
        /// <param name="cDRIndicator"> cDRIndicator </param>
        /// <param name="mTType"> mTType </param>
        /// <param name="retryOnFailReqTimes"> retryOnFailReqTimes </param>
        /// <param name="retryOnTimeOutReqTimes"> retryOnTimeOutReqTimes </param>
        /// <param name="retryOnFailReqDuration"> retryOnFailReqDuration </param>
        /// <param name="retryOnTimeOutReqDuration"> retryOnTimeOutReqDuration </param>
        /// <returns>bool</returns>
        public static bool Update(
            SqlGuid mtGuid,
            SqlGuid moGuid,
            SqlString shortCode,
            SqlString destAddr,
            SqlInt32 operatorID,
            SqlString message,
            SqlInt32 cooperateID,
            SqlString status,
            SqlDateTime sentTime,
            SqlString cDRIndicator,
            SqlString mTType,
            SqlInt32 retryOnFailReqTimes,
            SqlInt32 retryOnTimeOutReqTimes,
            SqlInt64 retryOnFailReqDuration,
            SqlInt64 retryOnTimeOutReqDuration)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "SmsMtStatistic_Update", 15);
            sph.DefineSqlParameter("@MtGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, mtGuid);
            sph.DefineSqlParameter("@MoGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, moGuid);
            sph.DefineSqlParameter("@ShortCode", SqlDbType.VarChar, 50, ParameterDirection.Input, shortCode);
            sph.DefineSqlParameter("@DestAddr", SqlDbType.VarChar, 15, ParameterDirection.Input, destAddr);
            sph.DefineSqlParameter("@OperatorID", SqlDbType.Int, ParameterDirection.Input, operatorID);
            sph.DefineSqlParameter("@Message", SqlDbType.NVarChar, 1000, ParameterDirection.Input, message);
            sph.DefineSqlParameter("@CooperateID", SqlDbType.Int, ParameterDirection.Input, cooperateID);
            sph.DefineSqlParameter("@Status", SqlDbType.VarChar, 50, ParameterDirection.Input, status);
            sph.DefineSqlParameter("@SentTime", SqlDbType.DateTime, ParameterDirection.Input, sentTime);
            sph.DefineSqlParameter("@CDRIndicator", SqlDbType.VarChar, 15, ParameterDirection.Input, cDRIndicator);
            sph.DefineSqlParameter("@MTType", SqlDbType.VarChar, 5, ParameterDirection.Input, mTType);
            sph.DefineSqlParameter("@RetryOnFailReqTimes", SqlDbType.Int, ParameterDirection.Input, retryOnFailReqTimes);
            sph.DefineSqlParameter("@RetryOnTimeOutReqTimes", SqlDbType.Int, ParameterDirection.Input, retryOnTimeOutReqTimes);
            sph.DefineSqlParameter("@RetryOnFailReqDuration", SqlDbType.BigInt, ParameterDirection.Input, retryOnFailReqDuration);
            sph.DefineSqlParameter("@RetryOnTimeOutReqDuration", SqlDbType.BigInt, ParameterDirection.Input, retryOnTimeOutReqDuration);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Deletes a row from the SmsMtStatistic table. Returns true if row deleted.
        /// </summary>
        /// <param name="mtGuid"> mtGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(
            SqlGuid mtGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "SmsMtStatistic_Delete", 1);
            sph.DefineSqlParameter("@MtGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, mtGuid);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Gets an IDataReader with one row from the SmsMtStatistic table.
        /// </summary>
        /// <param name="mtGuid"> mtGuid </param>
        public static IDataReader GetOne(
            SqlGuid mtGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "SmsMtStatistic_SelectOne", 1);
            sph.DefineSqlParameter("@MtGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, mtGuid);
            return sph.ExecuteReader();

        }

        /// <summary>
        /// Gets a count of rows in the SmsMtStatistic table.
        /// </summary>
        public static int GetCount(string sWhereCondition)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "SmsMtStatistic_GetCount", 1);
            sph.DefineSqlParameter("@WhereCondition", SqlDbType.NVarChar, ParameterDirection.Input, sWhereCondition);
            return Convert.ToInt32(sph.ExecuteScalar());

        }

        /// <summary>
        /// Gets an IDataReader with all rows in the SmsMtStatistic table.
        /// </summary>
        public static IDataReader GetAll()
        {

            return SqlHelper.ExecuteReader(
                GetReadConnectionString(),
                CommandType.StoredProcedure,
                "SmsMtStatistic_SelectAll",
                null);

        }

        /// <summary>
        /// Gets a page of data from the SmsMtStatistic table.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static IDataReader GetPage(
            string whereCondition,
            string orderBy,
            int pageNumber,
            int pageSize,
            out int totalPages)
        {
            totalPages = 1;
            int totalRows
                = GetCount(whereCondition);

            if (pageSize > 0) totalPages = totalRows / pageSize;

            if (totalRows <= pageSize)
            {
                totalPages = 1;
            }
            else
            {
                int remainder;
                Math.DivRem(totalRows, pageSize, out remainder);
                if (remainder > 0)
                {
                    totalPages += 1;
                }
            }

            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "SmsMtStatistic_SelectPage", 4);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            sph.DefineSqlParameter("@WhereCondition", SqlDbType.NVarChar, ParameterDirection.Input, whereCondition);
            sph.DefineSqlParameter("@OrderBy", SqlDbType.NVarChar, ParameterDirection.Input, orderBy);

            return sph.ExecuteReader();

        }

    }

}


